﻿using System.IO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//namespace Common.Extensions
//{
    public static class ObjectExtensions
    {
        public static void SafeClose(this Stream stream)
        {
            if (stream == null) return;
            if (stream.CanRead)
                stream.Close();
        }

        public static void SafeDispose<T>(this T obj) where T : IDisposable
        {
            if (obj != null)
            {
                obj.Dispose();
            }
        }
        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        public static bool IsNotNull(this object obj)
        {
            return obj != null;
        }

        public static string ConvertToNumber(this object number, string cultureName = "en-US")
        {
            var convertedNumber = string.Empty;

            var cultureInfo = new System.Globalization.CultureInfo(cultureName);

            if ( number.GetType() == typeof(int) )
                convertedNumber = ((int)number).ToString(cultureInfo);
            if ( number.GetType() == typeof(double) )
                convertedNumber = ((double)number).ToString(cultureInfo);
            if ( number.GetType() == typeof(float) )
                convertedNumber = ((float)number).ToString(cultureInfo);
            if ( number.GetType() == typeof(decimal) )
                convertedNumber = ((decimal)number).ToString(cultureInfo);

            return convertedNumber;
        }

        public static T Clone<T>(this T source)
        {
            var serialized = JsonConvert.SerializeObject(source);
            return JsonConvert.DeserializeObject<T>(serialized);
        }

        public static KeyValuePair<TKey, TValue> GetKeyValuePair<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
        {
            var keyValuePair = new KeyValuePair<TKey, TValue>();
            try
            {
                //return new KeyValuePair<TKey, TValue>(key, dictionary[key]);
                if ( dictionary.ContainsKey(key) )
                    keyValuePair = new KeyValuePair<TKey, TValue>(key, dictionary[key]);
            }
            catch (Exception)
            {
                throw;
            }
            return keyValuePair;
        }

        public static string GetExceptionDetails(this Exception exception)
        {
            var e = exception;
            var s = new StringBuilder();
            while (e != null)
            {
                s.AppendLine("Exception type: " + e.GetType().FullName);
                s.AppendLine("Message : " + e.Message);
                s.AppendLine("Stacktrace: ");
                s.AppendLine(e.StackTrace);
                s.AppendLine();
                e = e.InnerException;
            }
            return s.ToString();
        }
    }
//}
